﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TGB.Core.Infrastructure;
using TGBTech.Core.Models.DanhMuc;
using PagedList;

namespace TGBTech.WebAdmin.Controllers
{
    public class TaiKhoanController : Controller
    {
        private TGBTechDb db = new TGBTechDb();
        private const int pageSize = 100;

        // GET: TaiKhoan
        public ActionResult Index(int? page, string searchString)
        {
            // Select TaiKhoan have State = true
            var TaiKhoans = db.TaiKhoan.Where(p => p.State == true);

            // Search by string
            if (!String.IsNullOrEmpty(searchString))
            {
                TaiKhoans = TaiKhoans.Where(p => p.MaTaiKhoan.ToUpper().Contains(searchString.ToUpper())
                    || p.Name.ToUpper().Contains(searchString.ToUpper()));
            }

            // Orderby list
            TaiKhoans = TaiKhoans.OrderBy(p => p.State);
             
            // Calculate page index
            int index = TaiKhoans.Count() / pageSize + 1;
            int pageNumber = (page ?? index);
            
            return View(TaiKhoans.ToPagedList(pageNumber, pageSize));
        }


        // GET: TaiKhoan/Create
        public ActionResult Create()
        {
            ViewBag.List = db.TaiKhoan.Where(p => p.State == true).OrderByDescending(p => p.Id).Take(5).OrderBy(p => p.Id).ToList();
            return View();
        }

        // POST: TaiKhoan/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,MaTaiKhoan,Name,State")] TaiKhoan taiKhoan)
        {
            if (ModelState.IsValid)
            {
                taiKhoan.State = true;

                db.TaiKhoan.Add(taiKhoan);
                db.SaveChanges();


                return RedirectToAction("Create");
            }

            return View(taiKhoan);
        }

        // GET: TaiKhoan/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TaiKhoan taiKhoan = db.TaiKhoan.Find(id);
            if (taiKhoan == null)
            {
                return HttpNotFound();
            }
            return View(taiKhoan);
        }

        // POST: TaiKhoan/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,MaTaiKhoan,Name,State")] TaiKhoan taiKhoan)
        {
            if (ModelState.IsValid)
            {
                db.Entry(taiKhoan).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(taiKhoan);
        }

        // GET: TaiKhoan/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TaiKhoan taiKhoan = db.TaiKhoan.Find(id);
            if (taiKhoan == null)
            {
                return HttpNotFound();
            }
            return View(taiKhoan);
        }

        // POST: TaiKhoan/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            TaiKhoan taiKhoan = db.TaiKhoan.Find(id);
            taiKhoan.State = false;
            db.Entry(taiKhoan).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
